import numpy as np

def create_var4d_with_capture(var4d_method):
    # Create a subclass that will capture the latest analysis state
    class Var4DWithCapture(type(var4d_method)):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.latest_analysis = None
            
        def assimilate(self, HMM, xx, yy):
            original_assess = self.stats.assess
            
            # Override the assess method to capture analysis states
            def assess_with_capture(k, ko=None, typ=None, **kwargs):
                if typ == 'a' and 'mu' in kwargs:  # Capture analysis state
                    self.latest_analysis = kwargs['mu'].copy()
                return original_assess(k, ko, typ, **kwargs)
            
            self.stats.assess = assess_with_capture
            
            # Call the original assimilate method
            result = super().assimilate(HMM, xx, yy)
            
            # Restore original assess method
            self.stats.assess = original_assess
            
            return result
    
    # Create a new instance with our capturing functionality
    var4d_with_capture = Var4DWithCapture()
    for attr_name in dir(var4d_method):
        if not attr_name.startswith('__') and not callable(getattr(var4d_method, attr_name)):
            setattr(var4d_with_capture, attr_name, getattr(var4d_method, attr_name))
    
    return var4d_with_capture 